//
//  ClusterChatSource.m
//  HiQ
//
//  Created by arvintan on 12-8-9.
//  Copyright (c) 2012年 __MyCompanyName__. All rights reserved.
//

#import "ClusterChatSource.h"
#import "PacketDispathCenter.h"
#import "MainWindowController.h"
#import "MacrosDefine.h"
#import "Connection.h"
#import "Buddy.h"
#import "ImageAndTextCell.h"

@implementation ClusterChatSource

@synthesize _cluster;
@synthesize _delegate;

-(id) initWithCluster:(Cluster *) cluster
{
    self = [super init];
    _cluster = cluster;
//    [[PacketDispathCenter clusterChatArray] addObject:self];
    return self;
}

-(NSInteger) numberOfRowsInTableView:(NSTableView*) tv 
{
    return [[_cluster memberList] count];
}

-(id) tableView:(NSTableView *)tableView objectValueForTableColumn:(NSTableColumn *)tableColumn row:(NSInteger)row
{
    return nil;
}

//- (void)tableView:(NSTableView *)aTableView willDisplayCell:(id)aCell forTableColumn:(NSTableColumn *)aTableColumn row:(NSInteger)rowIndex
//{
//    if([[aTableColumn identifier] isEqualToString:@"ClusterMember"])
//    {
//        Buddy *buddy = [[_cluster memberList] objectAtIndex:rowIndex];
//        [(ImageAndTextCell*)aCell setImage:[NSImage imageNamed:buddy->photo]];
//        [(ImageAndTextCell*)aCell setStringValue:buddy->buddyName];
//    }
//}

-(void) onGetClusterMember
{
    NSUInteger groupID = [_cluster clusterId];
    NSData *data = [[NSData alloc] initWithBytes:&(groupID) length:sizeof(NSUInteger)];
    [[Connection sharedServerConnection] sendData:data cmd:CMD_FUN_GROUP subcmd:CMD_GROUP_MEMBERLIST_REQUEST];     
}

-(void) onReceiveClusterMemberPacket:(NSData *)data
{
    void *bytes = (void*)([data bytes]+24);
    
    CLUSTERMEMBERLISTREPLYHRD *replyHrd = (CLUSTERMEMBERLISTREPLYHRD*)bytes;
    NSUInteger error = replyHrd->error;
    
    if (error != GROUP_REQUEST_NO_ERROR) {
        NSLog(@"ClusterMemberReply error");
    }
    NSUInteger count = replyHrd->count;
    NSUInteger memberIDLen = replyHrd->memberIDLen;
    NSUInteger memberNameLen = replyHrd->memberNameLen;
    NSUInteger memberImageLen = replyHrd->memberImageLen;
    NSUInteger memberStatusLen = replyHrd->memberStatusLen;
    NSUInteger memberListenPortLen = replyHrd->memberListenPortLen;
    NSUInteger memberIpLen = replyHrd->memberIpLen;
    
    NSData *memberIDData = [[NSData alloc] initWithBytes:(bytes+sizeof(CLUSTERMEMBERLISTREPLYHRD)) length:memberIDLen];
    NSData *memberNameData = [[NSData alloc] initWithBytes:(bytes+sizeof(CLUSTERMEMBERLISTREPLYHRD)+memberIDLen) length:memberNameLen];
    NSData *memberImageData = [[NSData alloc] initWithBytes:(bytes+sizeof(CLUSTERMEMBERLISTREPLYHRD)+memberIDLen+memberNameLen)  length:memberImageLen];
    NSData *memberStatusData = [[NSData alloc] initWithBytes:(bytes+sizeof(CLUSTERMEMBERLISTREPLYHRD)+memberIDLen+memberNameLen+memberImageLen) length:memberStatusLen];
    NSData *memberListenPortData = [[NSData alloc] initWithBytes:(bytes+sizeof(CLUSTERMEMBERLISTREPLYHRD)+memberIDLen+memberNameLen+memberImageLen+memberStatusLen) length:memberListenPortLen];
    NSData *memberIpData = [[NSData alloc] initWithBytes:(bytes+sizeof(CLUSTERMEMBERLISTREPLYHRD)+memberIDLen+memberNameLen+memberImageLen+memberStatusLen+memberListenPortLen) length:memberIpLen];
    
    NSArray *memberIDArray = [NSKeyedUnarchiver unarchiveObjectWithData:memberIDData];
    NSArray *memberNameArray = [NSKeyedUnarchiver unarchiveObjectWithData:memberNameData];
    NSArray *memberImageArray = [NSKeyedUnarchiver unarchiveObjectWithData:memberImageData];
    NSArray *memberStatusArray = [NSKeyedUnarchiver unarchiveObjectWithData:memberStatusData];
    NSArray *memberListenPortArray = [NSKeyedUnarchiver unarchiveObjectWithData:memberListenPortData];
    NSArray *memberIpArray = [NSKeyedUnarchiver unarchiveObjectWithData:memberIpData];
    
    if ([[_cluster memberList] count] != 0) {
        [[_cluster memberList] removeAllObjects];
    }
    
    for (NSUInteger i = 0; i < count; ++i) {
        Buddy *buddy = [[Buddy alloc] init];
        buddy.buddyQQ = [[memberIDArray objectAtIndex:i] integerValue];
        buddy.buddyName = [memberNameArray objectAtIndex:i];
        buddy.photo = [memberImageArray objectAtIndex:i];
        buddy.status = [[memberStatusArray objectAtIndex:i] integerValue];
        buddy->listenPort = [[memberListenPortArray objectAtIndex:i] intValue];
        buddy->ip = [memberIpArray objectAtIndex:i];
        buddy.onPhoto = [NSImage imageNamed:buddy.photo];
        buddy.offPhoto = GrayImageFromNSImageWithAlpha(buddy.onPhoto);
        [[_cluster memberList] addObject:buddy];
    }
    
    dispatch_async(dispatch_get_main_queue(), ^{
        [_delegate getMemberList];
    });
}

-(void) onSendChatMsg:(NSString *)msg Name:(NSString*)name
{
    NSString *pairMsg = [NSString stringWithFormat:@"%lu/%@/%lu/%@/%@",_cluster.clusterId,_cluster.clusterName,[[MainWindowController shareMyselfInfo] buddyQQ],name,msg];
    [[Connection sharedServerConnection] sendMsg:pairMsg cmd:CMD_FUN_GROUPCHAT subcmd:CMD_GROUPCHAT_SEND];
}


-(void) onreceiveChatMsg:(NSString *)msg Name:(NSString*)name
{
    dispatch_async(dispatch_get_main_queue(), ^{
        [_delegate displayChatMessage:msg Name:name];
    });
}

@end
 